package com.itheima.prize.commons.db.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.prize.commons.BO.CardUserBO;
import com.itheima.prize.commons.constant.AuthMessageConstants;
import com.itheima.prize.commons.constant.RedisConstants;
import com.itheima.prize.commons.convert.CardUserBOConverter;
import com.itheima.prize.commons.db.entity.CardGameProduct;
import com.itheima.prize.commons.db.entity.CardUser;
import com.itheima.prize.commons.db.entity.CardUserGame;
import com.itheima.prize.commons.db.entity.CardUserHit;
import com.itheima.prize.commons.db.mapper.CardGameProductMapper;
import com.itheima.prize.commons.db.mapper.CardUserGameMapper;
import com.itheima.prize.commons.db.mapper.CardUserHitMapper;
import com.itheima.prize.commons.db.service.CardUserService;
import com.itheima.prize.commons.db.mapper.CardUserMapper;
import com.itheima.prize.commons.utils.ApiResult;
import com.itheima.prize.commons.utils.PasswordUtil;
import com.itheima.prize.commons.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author shawn
 * @description 针对表【card_user(会员信息表)】的数据库操作Service实现
 * @createDate 2023-12-26 11:58:48
 */
@Service
@Slf4j
public class CardUserServiceImpl extends ServiceImpl<CardUserMapper, CardUser>
        implements CardUserService {

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private CardUserGameMapper cardUserGameMapper;

    @Resource
    private CardUserHitMapper cardUserHitMapper;

    /**
     * 校验用户账号密码
     */
    @Override
    public ApiResult CheckAccountAndPassword(String account, String password) {
        LambdaQueryWrapper<CardUser> wrapper = new LambdaQueryWrapper<CardUser>().eq(CardUser::getUname, account);
        CardUser cardUser = this.getOne(wrapper);
        if (Objects.isNull(cardUser)) {
            log.info("未查询到账号信息");
            return null;
        }
        // 账号存在，校验密码是否通过密码+盐的方式匹配数据库的md5密码
        String newPassword = PasswordUtil.encodePassword(PasswordUtil.encodePassword(password) + cardUser.getSalt());
        String oldPassword = PasswordUtil.encodePassword(cardUser.getPasswd() + cardUser.getSalt());
        if (!newPassword.equals(oldPassword)) {
            // 校验是否达到试错上限
            Object tryNum = redisUtil.get(RedisConstants.LOGIN_PREFIX + cardUser.getId());
            if (!Objects.isNull(tryNum) && tryNum == RedisConstants.MAX_LOGIN_ATTEMPTS) {
                // 设置过期时间
                redisUtil.expire(RedisConstants.LOGIN_PREFIX + cardUser.getId(), RedisConstants.LOCK_TIME_DURATION);
                return ApiResult.error(AuthMessageConstants.LOGIN_ERROR_FIVE_TIMES, null);
            }
            // 上锁+1
            redisUtil.incr(RedisConstants.LOGIN_PREFIX + cardUser.getId(), RedisConstants.INIT_NUM);
            return ApiResult.error(AuthMessageConstants.LOGIN_ERROR, null);
        }
        cardUser.setPasswd("");
        cardUser.setSalt("");

        return ApiResult.success(cardUser);
    }

    @Override
    public CardUserBO getCardUserInfo(CardUser cardUser) {
        CardUserBO cardUserBO = CardUserBOConverter.INSTANCE.convertCardUserToBO(cardUser);

        // 获取用户参与的活动数量
        LambdaQueryWrapper<CardUserGame> wrapper = new LambdaQueryWrapper<CardUserGame>()
                .eq(CardUserGame::getUserid, cardUser.getId());
        Long games = cardUserGameMapper.selectCount(wrapper);
        cardUserBO.setGames(Math.toIntExact(games));

        // 获取用户的中奖数
        LambdaQueryWrapper<CardUserHit> userHitLambdaQueryWrapper = new LambdaQueryWrapper<CardUserHit>()
                .eq(CardUserHit::getUserid, cardUser.getId());
        Long hitCount = cardUserHitMapper.selectCount(userHitLambdaQueryWrapper);
        cardUserBO.setProducts(Math.toIntExact(hitCount));

        return cardUserBO;
    }
}




